package dynamic

import (
	"encoding/json"
	"io/ioutil"
	"log"
	"meetroom/server/manage"
	"path/filepath"
	"time"

	"github.com/garyburd/redigo/redis"
	"github.com/jinzhu/gorm"
)

//TODO:包内共享的配置文件信息
const configfile = "dynamic/config.json"

var configData DynamicConfig=DynamicConfig{
	GlobalJS: make([]string, 0)	,
	GlobalCss: make([]string, 0),
	Apps: make(map[string]AppDivLevel),
}

func DynamicApplication(db *gorm.DB,rC *redis.Conn) manage.Application {
	app := manage.NewApplication("/page", "dynamic", "")
	go loadConfig()

	app.AsignViewer(dynamicView(db,rC))

	return app
}

func loadConfig() {
	var temp DynamicConfig
	for {
		path,_:=filepath.Abs(configfile)
		file, err := ioutil.ReadFile(path)
		if err != nil {
			continue
		}
		err = json.Unmarshal(file, &temp)

		if err != nil {
			continue
		} else {
			configData = temp
		}
		log.Println("load Cnofig Done")
		time.Sleep(15 * time.Second)
	}


}
